<template>
  <div class="box">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/page' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-input
        style="width: 30%"
        placeholder="请输入内容"
        class="input-with-select"
      >
        <el-button slot="append" icon="el-icon-search"></el-button>
      </el-input>
      <el-button
        style="margin-left: 20px"
        type="primary"
        @click="$router.push('/add')"
        >添加商品</el-button
      >
      <el-table :data="tableData" border style="width: 100%; margin-top: 20px">
        <el-table-column type="index" label="#" width="80"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="720">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="200">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="upen(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="del(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagenum"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { goods, categories, deletegoods } from '@/api/login.js'
export default {
  data() {
    return {
      pagesize: 5,
      pagenum: 1,
      total: 0,
      tableData: [],
    }
  },
  mounted() {
    this.getlist()
  },
  methods: {
    async getlist() {
      let res = await goods(this.pagenum, this.pagesize)
      // console.log(res.data.data.goods)
      res.data.data.goods.forEach((item) => {
        var unixTimestamp = new Date(item.add_time * 1000)
        let commonTime = unixTimestamp.toLocaleString()
        item.add_time = commonTime
        this.tableData = res.data.data.goods
        this.total = res.data.data.total
      })
    },
    handleSizeChange(siae) {
      // console.log(siae)
      this.pagesize = siae
      this.getlist()
    },
    // 分页的实现
    handleCurrentChange(page) {
      // console.log(page)
      this.pagenum = page
      this.getlist()
    },
    upen(e) {
      // console.log(e)
    },
    del(e) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          deletegoods(e.goods_id).then((res) => {
            // console.log(res)
            this.$message({
              type: 'success',
              message: res.data.meta.msg,
            })
            this.getlist()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
  },
  computed: {},
  watch: {},
}
</script>

<style scoped lang="scss">
.box {
  padding: 20px;
  .box-card {
    margin-top: 20px;
  }
}
</style>
